import Vuex from 'vuex'

const setLocalCartList = (state) => {
  const { cartList } = state
  const cartListString = JSON.stringify(cartList) // 转换为JSON字符串
  localStorage.cartList = cartListString // localStorage本地存储
}

const getLocalCartList = () => {
  // JSON.parse规则的字符串转换为 JavaScript 对象。
  // const cartListObj = localStorage.cartList ? JSON.parse(localStorage.cartList) : {}
  try {
    return JSON.parse(localStorage.cartList)
  } catch (e) {
    return {}
  }
}

export default Vuex.createStore({
  state: {
    cartList: getLocalCartList()
    // cartList: {
    //  // 第一层是店铺id
    //  shopId: {
    //    shopName: '沃尔玛',
    //    productList: {
    //      第三层是商品id
    //      productId: [{
    //        // 第四层是商品内容以及购物数量
    //        _id: '1',
    //        name: '番茄250g/份',
    //        imgUrl: 'http://www.dell-lee.com/imgs/vue3/tomato.png',
    //        sales: 10,
    //        price: 33.6,
    //        oldPrice: 39.6
    //      }]
    //    }
    //  }
    // }
  },
  mutations: {
    // 加(减)入购物车1 处理产品相关信息 payload 传递过来的数据
    changeCartItemInfo (state, payload) {
      const { shopId, productId, productInfo } = payload
      var shopInfo = state.cartList[shopId] || {
        shopName: '', productList: {}
      }
      let product = shopInfo.productList[productId]
      if (!product) {
        productInfo.count = 0
        product = productInfo
      }
      product.count = product.count + payload.num
      // 是否为第一次加都选中状态
      if (payload.num > 0) { product.check = true }
      // 数量小于0
      if (product.count < 0) { product.count = 0 }
      shopInfo.productList[productId] = product
      // // 数量小于1从数组中移除
      // if (product.count < 1) {
      //   delete shopInfo.productList[productId]
      // }
      state.cartList[shopId] = shopInfo
      setLocalCartList(state) // 持久存储
    },
    // 加(减)入购物车2 加入店铺名
    changeShopName (state, payload) {
      const { shopId, shopName } = payload
      const shopInfo = state.cartList[shopId] || {
        shopName: '', productList: {}
      }
      shopInfo.shopName = shopName
      state.cartList[shopId] = shopInfo
      setLocalCartList(state) // 持久存储
    },
    // 购物车列表单项数据选中和反选中
    changeCartItemChecked (state, payload) {
      const { shopId, productId } = payload
      const product = state.cartList[shopId].productList[productId]
      product.check = !product.check
      setLocalCartList(state) // 持久存储
    },
    // 清空购物车数据
    clearCartProduct (state, payload) {
      const { shopId } = payload
      state.cartList[shopId].productList = {}
      setLocalCartList(state) // 持久存储
    },
    // 购物车头部全选->列表全为选中状态
    setCarItemsChecked (state, payload) {
      const { shopId } = payload
      const products = state.cartList[shopId].productList
      const isAllChecked = payload.isAllChecked
      if (products) {
        for (const key in products) {
          const product = products[key]
          product.check = isAllChecked
        }
      }
      setLocalCartList(state) // 持久存储
    },
    // 提交订单清除数据
    clearCartData (state, shopId) {
      state.cartList[shopId].productList = {}
    }
  },
  actions: {
  },
  modules: {
  }
})
